<?php


namespace App\Http\Controllers\Admin;


use App\Http\Controllers\Admin\Traits\AdminTrait;
use App\Http\Requests\Admin\BaseRequest;
use App\Models\Admin\AdminOperateLogs;
use App\Models\Admin\AdminSetting;
use App\Models\Admin\Commission;
use App\Models\Admin\Company;
use App\Models\Admin\Document;
use App\Models\Admin\DomesticBill;
use App\Models\Admin\Order;
use App\Models\Admin\RenewalOrder;
use App\Notifications\SiteNotice;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

class CommissionController extends AdminController
{
    use AdminTrait;
    /**
     * 定义验证规则
     */

    protected $pay_status_List=[
        '0'=>'待支付',
        '1'=>'已支付',
        '2'=>'已作废',
    ];

    protected $model;
    public function __construct(Commission $model)
    {
        parent::__construct();
        $this->model = $model;
    }

    protected $type_list=[
        1=>'订单',
        2=>'结差',
    ];

    protected $validate_rule = [
    ];

    /**
     * 定义验证错误信息
     */
    protected $err_msg = [
    ];

    protected function beforeIndex()
    {
        $data=[
            'type_list'=>$this->type_list
        ];
        return $data;
    }

    protected function search($request)
    {
        $select = $this->model->with(['company']);

        $create_start_time = $request->query('create_start_time','');
        $create_end_time = $request->query('create_end_time','');
        if ($create_start_time && $create_end_time){
            $select = $select->whereBetween('created_at',[$create_start_time,$create_end_time+86400]);
        }

//        $company_name = $request->query('company_name','');
//        if ($company_name){
//            $company_uids = Company::where('company_name','like',"%$company_name%")->pluck('id');
//            $select = $select->whereIn('company_id',$company_uids);
//        }
        ($order_sn = $request->query('order_sn','')) && $select = $select->where('order_sn','like',"%$order_sn%");
        ($type = $request->query('type',0)) && $select = $select->where('type',$type);
        ($company_id = $request->query('company_id',0)) && $select = $select->where('company_id',$company_id);

        return $select;
    }

    public function offlineCommissionList(BaseRequest $request)
    {
        $input=$request->all();
        //过滤数据
        $this->vali_rules['payment_days']='date';
        $this->vali_rules['order_sn']='string';
        $this->vali_rules['company_id']='exists:company,id';
        $this->vali_rules['created_time_start']='date';
        $this->vali_rules['created_time_end']='date|after_or_equal:created_time_start';
        $this->vali_rules['pay_status']='in:0,1,2';
        $validator = Validator::make($input, $this->vali_rules,$this->vali_msg);
        if($validator->fails())
            return $this->error(['-1',$validator->errors()->first()]);
        $this->getPageAndSize($input);
        $list=DomesticBill::getList($input,$this->skip,$this->size);
        $count=DomesticBill::getCount($input);
        $data=[
            'list'=>$list,
            'count'=>$count,
            'pay_status_List'=>$this->pay_status_List,
        ];
        return  $this->success($data);
    }

    public function offlineCommissionDetail(BaseRequest $request)
    {
        $input=$request->all();
        $rules=[
            'id'=>[
                'required',
                Rule::exists('domestic_bill','id'),
            ]
        ];
        $validator = Validator::make($input,$rules,$this->vali_msg);
        if($validator->fails())
            return $this->error(['-1',$validator->errors()->first()]);
        $id=$input['id'];
        $info=DomesticBill::getDetail($id);
        return $this->success($info);
    }

    public function addBill(BaseRequest $request)
    {
        $input=$request->all();
        $this_month=date('Y-m-01');
        $rules=[
            'company_id'=>[
                'required',
                Rule::exists('company','id')->where('model',2),
            ],
            'payment_days'=>"required|date|before:$this_month"
        ];
        $msg=[
        ];
        $validator = Validator::make($input,$rules,$this->vali_msg);
        if($validator->fails())
            return $this->error(['-1',$validator->errors()->first()]);
        $company_id=$input['company_id'];
        $payment_days=date('Y-m',strtotime($input['payment_days']));
        $payment_days_tz=date('Y/m',strtotime($input['payment_days']));
        //判断所选账期内是否有账单
        $bill = DomesticBill::where('payment_days',$payment_days)->where('company_id',$company_id)->where('pay_status','<',2)->count();
        if($bill) return $this->error(['-1','所选账期内已有账单']);
        //判断有配送金额，没有则不生成
        $begin_time = strtotime($payment_days);//账期开始时间
        $end_time = mktime(23, 59, 59, date('m', strtotime($payment_days))+1, 00);
        $send_money = Order::where('company_id',$company_id)->where('is_prior_payment',0)->whereIn('order_status',[1,2,3])->whereBetween('send_time',[$begin_time,$end_time])->sum("send_money");
        $return_money = Order::where('company_id',$company_id)->where('is_prior_payment',0)->whereIn('order_status',[1,2,3])->whereBetween('send_time',[$begin_time,$end_time])->sum("return_money");
        $send_money=$send_money-$return_money;
        if ($send_money <= 0) return $this->error(['-1','所选账期内没有配送金额']);
        //判断客户在账期内是否到期
        $company_info=Company::where('id',$company_id)->select('id','end_time','commission_rate')->first();
        if($company_info->end_time<$end_time) return $this->error(['-1','客户在该账期内合约已经到期']);
        $commission_rate = str_replace("‰","",$company_info->commission_rate);
        $commission = round(($send_money*$commission_rate)/1000,2);
        $today_stamp=strtotime(date('Y-m-d'));
        $admin_setting=AdminSetting::pluck('value','name');
        $pay_notice_days=$admin_setting['pay_notice_days']?? 6;
        $pay_forbid_days=$admin_setting['pay_forbid_days']?? 7;
        $order_sn=$this->sn_job('YJZD');
        $today_tz=date('Y/m/d');
        $data_add=[
            'company_id' => $company_id,
            'order_sn' => $order_sn,
            'payment_days' => $payment_days,
            'original_send_money' => $send_money,
            'original_commission' => $commission,
            'original_commission_rate' => $company_info->commission_rate,
            'send_money' => $send_money,
            'commission' => $commission,
            'commission_rate' => $company_info->commission_rate,
            'zero_money' => 0,
            'real_money' => $commission,
            'zero_status' => 0,
            'pay_status' => 0,
            'notice_days' => $today_stamp+($pay_notice_days*86400),//取配置
            'forbid_days' => $today_stamp+($pay_forbid_days*86400),//取配置
            'created_at' => time(),
            'updated_at' => time(),
        ];
        DB::beginTransaction();
        try {
            $info=DomesticBill::create($data_add);
            DomesticBill::addLog($info->toArray(),[],1);
            //发送通知
            $doc_data=[
                'type'=>2,
                'title'=>config('app.zd_title_create'),
                'status'=>1,
                'author'=>'菜东家集团',
                'content'=>"<p>尊敬的菜东家合伙人，您好<br />&nbsp; &nbsp;&nbsp;<br />&nbsp; &nbsp; 您{$payment_days_tz}账期的货到付款佣金账单已生成，账单号{$order_sn}，请及时对账和付款，以免影响系统的正常使用，感谢您的支持！<br />&nbsp; &nbsp; &nbsp;&nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;菜东家集团<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{$today_tz}</p>",
                'product_type'=>2,
                'document_type'=>5,
            ];
            $res_tz=Document::create($doc_data);
            $company_info->notify(new SiteNotice($res_tz));
            DB::commit();
            return $this->success();
        }
        catch (\Exception $exception)
        {
            DB::rollBack();
            return  $this->error();
        }
    }

    //作废账单
    public function invalidBill(BaseRequest $request)
    {
        $input=$request->all();
        $rules=[
            'id'=>[
                'required',
                Rule::exists('domestic_bill','id')->where('pay_status',0),
            ]
        ];
        $validator = Validator::make($input,$rules,$this->vali_msg);
        if($validator->fails())
            return $this->error(['-1',$validator->errors()->first()]);
        $id=$input['id'];
        DB::beginTransaction();
        try {
            $info=DomesticBill::find($id);
            $perInfo = $info->getOriginal();//获取修改之前的字段
            $info->pay_status=2;
            $info->save();
            $nextInfo = $info->getChanges();//获取修改之后的字段
            DomesticBill::addLog($perInfo,$nextInfo,2,__FUNCTION__);
            $company_info=Company::where('id',$info->company_id)->select('id','model')->first();
            $date_tz=date('Y/m/d');
            //发送通知
            $doc_data=[
                'type'=>$company_info->model,
                'title'=>config('app.zd_title_cancel'),
                'status'=>1,
                'author'=>'菜东家集团',
                'content'=>"<p>尊敬的菜东家合伙人，您好<br />&nbsp; &nbsp;&nbsp;<br />&nbsp; &nbsp; 您账单号为{$info->order_sn}的货到付款佣金账单已作废，请知悉，带来不便，敬请凉解！</p>
<p>&nbsp; &nbsp; &nbsp;&nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;菜东家集团<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{$date_tz}</p>",
                'product_type'=>2,
                'document_type'=>5,
            ];
            $res_tz=Document::create($doc_data);
            $company_info->notify(new SiteNotice($res_tz));
            DB::commit();
            return  $this->success();
        }
        catch (\Exception $exception)
        {
            DB::rollBack();
            return  $this->error();
        }
    }

    //对账
    public function account(BaseRequest $request)
    {
        $input=$request->except('sign','timeStamp');
        $rules=[
            'id'=>[
                'required',
                Rule::exists('domestic_bill','id')->where('pay_status',0),
            ],
            'send_money'=>'required|numeric',
            'commission_rate'=>'required|integer',
            'commission'=>'required|numeric',
        ];
        $validator = Validator::make($input,$rules,$this->vali_msg);
        if($validator->fails())
            return $this->error(['-1',$validator->errors()->first()]);
        $id=$input['id'];
        $info=DomesticBill::where('id',$id)->select('zero_status')->first();
        if($info->zero_status)
        {
            return $this->error(['-1','账单已经抹零，不能对账']);
        }
        DB::beginTransaction();
        try {
            $info=DomesticBill::find($id);
            $perInfo = $info->getOriginal();//获取修改之前的字段
            $info->send_money=$input['send_money'];
            $info->commission_rate=$input['commission_rate'].'‰';
            $info->commission=$input['commission'];
            $info->real_money=$input['commission'];
            $info->save();
            $nextInfo = $info->getChanges();//获取修改之后的字段
            DomesticBill::addLog($perInfo,$nextInfo,2,__FUNCTION__);
            DB::commit();
            return  $this->success();
        }
        catch (\Exception $exception)
        {
            DB::rollBack();
            return  $this->error();
        }
    }

    //抹零
    public function zero(BaseRequest $request)
    {
        $input=$request->except('sign','timeStamp');
        $rules=[
            'id'=>[
                'required',
                Rule::exists('domestic_bill','id')->where('pay_status',0),
            ],
            'zero_money'=>'required|numeric',
            'real_money'=>'required|numeric',
        ];
        $validator = Validator::make($input,$rules,$this->vali_msg);
        if($validator->fails())
            return $this->error(['-1',$validator->errors()->first()]);
        $id=$input['id'];
        DB::beginTransaction();
        try {
            $info=DomesticBill::find($id);
            $perInfo = $info->getOriginal();//获取修改之前的字段
            $info->zero_money=$input['zero_money'];
            $info->real_money=$input['real_money'];
            $info->zero_status=1;
            $info->save();
            $nextInfo = $info->getChanges();//获取修改之后的字段
            DomesticBill::addLog($perInfo,$nextInfo,2,__FUNCTION__);
            DB::commit();
            return  $this->success();
        }
        catch (\Exception $exception)
        {
            DB::rollBack();
            return  $this->error();
        }
    }

    //设置
    public function setBill(BaseRequest $request)
    {
        $input=$request->except('sign','timeStamp');
        $rules=[
            'id'=>[
                'required',
                Rule::exists('domestic_bill','id')->where('pay_status',0),
            ],
            'notice_days'=>'required|integer',
            'forbid_days'=>'required|integer|gt:notice_days',
        ];
        $msg=[
            'forbid_days.gt'=>'关闭天数必须大于提醒天数',
        ];
        $validator = Validator::make($input,$rules,$this->vali_msg+$msg);
        if($validator->fails())
            return $this->error(['-1',$validator->errors()->first()]);
        $id=$input['id'];
        $info=DomesticBill::where('id',$id)->select('created_at')->first();
        $created_time=strtotime($info->created_at->format("Y-m-d"));
        $input['notice_days']=$created_time+($input['notice_days']*86400);
        $input['forbid_days']=$created_time+($input['forbid_days']*86400);
        DB::beginTransaction();
        try {
            $info=DomesticBill::find($id);
            $perInfo = $info->getOriginal();//获取修改之前的字段
            $info->notice_days=$input['notice_days'];
            $info->forbid_days=$input['forbid_days'];
            $info->save();
            $nextInfo = $info->getChanges();//获取修改之后的字段
            DomesticBill::addLog($perInfo,$nextInfo,2,__FUNCTION__);
            DB::commit();
            return  $this->success();
        }
        catch (\Exception $exception)
        {
            DB::rollBack();
            return  $this->error();
        }
    }

    //获取天数
    public function getBillDays(BaseRequest $request)
    {
        $input=$request->all();
        $rules=[
            'id'=>[
                'required',
                Rule::exists('domestic_bill','id'),
            ],
        ];
        $validator = Validator::make($input,$rules,$this->vali_msg);
        if($validator->fails())
            return $this->error(['-1',$validator->errors()->first()]);
        $id=$input['id'];
        $info=DomesticBill::where('id',$id)->select('forbid_days','notice_days','created_at')->first();
        $created_time=strtotime($info->created_at->format("Y-m-d"));
        $forbid_days_num=intval(($info->forbid_days-$created_time)/86400);
        $notice_days_num=intval(($info->notice_days-$created_time)/86400);
        $data=[
          'forbid_days'=>$forbid_days_num,
          'notice_days'=>$notice_days_num,
        ];
        return  $this->success($data);
    }

    public function settlement(BaseRequest $request)
    {
        $input=$request->all();
        $rules=[
            'id'=>[
                'required',
                Rule::exists('domestic_bill','id')->where('pay_status',0),
            ],
            'collection_channels'=>'required|in:2,3,4,5',
            'payer'=>'required',
            'remark'=>'',
            'credentials'=>'',
        ];
        $validator = Validator::make($input,$rules,$this->vali_msg);
        if($validator->fails())
            return $this->error(['-1',$validator->errors()->first()]);
        $id=$input['id'];
        DB::beginTransaction();
        try {
            $info=DomesticBill::find($id);
            $perInfo = $info->getOriginal();//获取修改之前的字段
            $info->collection_channels=$input['collection_channels'];
            $info->payer=$input['payer'];
            $info->remark=$input['remark']??'';
            $info->credentials=$input['credentials']??'';
            $info->pay_status=1;
            $info->pay_type=2;
            $info->save();
            $nextInfo = $info->getChanges();//获取修改之后的字段
            DomesticBill::addLog($perInfo,$nextInfo,6,__FUNCTION__);
            $company_info=Company::where('id',$info->company_id)->select('id','model')->first();
            $date_tz=date('Y/m/d');
            //发送通知
            $payment_tz=str_replace('-','/',$info->payment_days);
            $doc_data=[
                'type'=>$company_info->model,
                'title'=>config('app.zd_title_count'),
                'status'=>1,
                'author'=>'菜东家集团',
                'content'=>"<p>尊敬的菜东家合伙人，您好<br />&nbsp; &nbsp;&nbsp;<br />&nbsp; &nbsp; 您已结算{$payment_tz}账期，账单号为{$info->order_sn}的货到付款佣金账单，谢谢您的配合！<br />&nbsp; &nbsp; &nbsp;&nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;菜东家集团<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{$date_tz}</p>",
                'product_type'=>2,
                'document_type'=>5,
            ];
            $res_tz=Document::create($doc_data);
            $company_info->notify(new SiteNotice($res_tz));
            DB::commit();
            return  $this->success();
        }
        catch (\Exception $exception)
        {
            DB::rollBack();
            return $this->error();
        }

    }


    //生命周期
    public function getBillLife(BaseRequest $request)
    {
        $input=$request->all();
        $rules=[
            'id'=>[
                'required',
                Rule::exists('domestic_bill','id'),
            ],
        ];
        $validator = Validator::make($input,$rules,$this->vali_msg);
        if($validator->fails())
            return $this->error(['-1',$validator->errors()->first()]);
        $id=$input['id'];
        $list=AdminOperateLogs::getListByMainId($id,'domestic_bill');
        $data=[
            'list'=>$list
        ];
        return  $this->success($data);
    }

    public function getRenewOrder(BaseRequest $request)
    {
        $page = $request->query('page') ?? 1;
        $per_page = $request->query('per_page') ?? 20;
        $start_time = $request->query('start_time');
        $end_time = $request->query('end_time');
        $select = RenewalOrder::with(['company:id,company_name'])->select('id','company_id','order_sn','money','pay_time','created_at','pay_status');
        ($company_id = $request->query('company_id')) && $select = $select->where('company_id',$company_id);
        ($order_sn = $request->query('order_sn')) && $select = $select->where('order_sn','like',"%$order_sn%");
        if ($start_time && $end_time) {
            $select = $select->whereBetween('created_at',date_assemble($start_time,$end_time));
        }
        $select = $select->where('pay_status',1);
        $count = $select->count();
        $list = $select->orderBy('created_at','desc')->skip(($page-1)*$per_page)->limit($per_page)->get();
        return $this->success(['count'=>$count,'list'=>$list]);
    }
}
